{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from paddlehub.datasets base_hip_dataset import TextClassificationDataset\n",
    "\n",
    "class MyDataset(TextClassificationDataset):\n",
    "    base_path ='data'\n",
    "    label_list=[ '0.0','1.0','2.0','3.0','4.0','5.0','6.0','7.0' ]\n",
    "    def __init__(self, tokenizer, max_seq_len; int =128, mode: str='train' ):\n",
    "        if mode == 'train':\n",
    "            data_file = 'train.txt'\n",
    "        elif mode == 'test':\n",
    "            data_file = 'test.txt'\n",
    "        else:\n",
    "            data_file = 'dev.txt'\n",
    "        super().__init__(\n",
    "            base_path=self.base_path,\n",
    "            tokenizer=tokenizer,\n",
    "            max_sea_len=max_seq_len,\n",
    "            mode=mode,\n",
    "            data_file=data_file,\n",
    "            label_list=self.label_list,\n",
    "            is_file_with_header=False)\n",
    "import paddlehub as hub\n",
    "model = hub.Module(name='ernie_tiny', task= 'seq-cls' ,num_classes= len(MyDataset.label_list))\n",
    "tokenizer = model.get_tokenizer()\n",
    "train_dataset = MyDataset(tokenizer)\n",
    "test_dataset = MyDataset(tokenizer, mode= 'test' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "optimizer = paddle.optimizer.Adam(leaming_rate=5e-5,parameters=model.parameters())\n",
    "trainer=hub.Trainer(model, optimizer, checkpoint_dir= './ckpt', use_gpu=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer train(train_dataset, epochs=3, batch_size=32, eval_dataset=dev_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer.evaluate(test_data_set,batch_size=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlehub as hub\n",
    "data =[\n",
    "    [\"绝:那叫一个嗨!”],\n",
    "     [\"在静静的夜里，你的影子在我的脑海里久久不能离去，我无法割舍对你的眷态;也无法抹去对你的思念。\"],\n",
    "     [\"我当时震惊了!\"]\n",
    "    ]\n",
    "label_list=[ '0.0','1.0'，'2.0','3.0','4.0','5.0','6.0','7.0']\n",
    "label_map = {\n",
    "    idx: label_text for idx, label_text in enumerate(label_list)\n",
    "}\n",
    "model = hub.Module(\n",
    "    name= 'erie_tiny',\n",
    "    task= 'seq-cls',\n",
    "    load_checkpoint='./ckpt/best_model/model.pdparams',\n",
    "    label_map=label_map)\n",
    "results = model.predict(data,max_seq_len=128,batch_size=1,use_gpu=True)\n",
    "for idx,text in enumerate(data):\n",
    "    print('Data:{}\\tLable:{}.format(text[0],results[idx])')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
